package easypsync.ics;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Calendar {
    List<Event> events = new ArrayList<Event>();

    // This is to retain the non interpretable lines
    List<String> rest = new ArrayList<String>();

    public Calendar() {

    }

    public Calendar(InputStream in) {
//        read(in);
    }

    public void addEvent(Event e) {
    	events.add(e);
    }
    
    public void write(OutputStream out) throws IOException {
        BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(out), 8192);
        bf.write("BEGIN:VCALENDAR\r\n");
        bf.flush();
        for (Event event : events) {
            event.write(out);
        }
        for (String extraline : rest) {
            bf.write(extraline);
            bf.write("\r\n");
        }
        bf.write("END:VCALENDAR");
        bf.flush();
    }

    public void read(InputStream in) throws IOException, ParseException {
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String line;
        while ((line = br.readLine()) != null) {
            if (line.contains("END:VCALENDAR")) {
                break;
            } else if (line.startsWith("BEGIN:VEVENT")) {
                Event e = new Event();
                e.read(br);
                events.add(e);
            } else
                rest.add(line);
        }
    }
    
    public String toString() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            write(baos);
            return baos.toString();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }
    
 // TEST //
    public static void main(String[] args) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
        Calendar c = new Calendar();
        c.rest.add("NOTKNOWN:anyvalue");
        Event e1 = new Event(new Date(), new Date(), "hkjhk2");
        Event e2 = new Event(new Date(), new Date(), "hkjhk2");
        
        c.events.add(e1);
        c.events.add(e2);
        
        System.out.println(c);
        try {
            c.write(baos);
            byte[] b = baos.toByteArray();
            
            ByteArrayInputStream bais = new ByteArrayInputStream(b);
            
            Calendar c2 = new Calendar(); 
            c2.read(bais);
            bais.close();
            baos.close();
            
            System.out.println(c2);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
